uid |
---|
NetCode.DynamicData.DynamicEntityProperties |
Whenever you create a content-type - like Person - and want to work with the data in your C# Razor templates, you'll be working with a Dynamic Entity. You can get any value using one of the following:
- the dot-notation like
Content.FirstName
- the
Get()
method likeContent.Get("FirstName")
- the
Get()
method with a fallback value likeContent.Get("FirstName", fallback: "John")
- the typed
Get<T>()
method likeContent.Get<string>("FirstName")
👉 also read about
[!include""]
Technically the dynamic entity object is like a read-helper for the more complex . So actually the dynamic entity object will keep a reference to the underlying read-only IEntity
item on a property Entity
, and whenever your code accesses a property, the dynamic entity will query it from the underlying Entity
.
The main things that the dynamic entity does for you, are
- Give you a nice, short syntax to access a property - so
Content.FirstName
instead ofObject.Attributes["FirstName"]["en"]
which would be necessary using the more advancedIEntity
object - Ensure that the language used in retrieving a value is the current user language
- Give conveniant access to related information like the
Presentation
object - Automatically handle some data-not-found errors
- Automatically do conversions, like convert related entities (like
.Children
) into dynamic objects to make your code more consistant
Internally there are a few things that can returned if you do something like Content.SomeProperty
- If the
SomeProperty
is one of the internal properties likeEntityId
etc. (see below) this will be returned - Next is a simple property of the underlying Entity,
- like
FirstName
which would be a string - or a relationship property like
Tags
which will return a special DynamicEntity that behaves as a list (see below)
- like
- Last but not least - if nothing matches, it's
null
Read the API docs in the .
Additional properties that work (they are dynamic, so don't appear in the code)
- EntityId int
- EntityGuid Guid
- EntityType string - the type name like Person
- IsPublished bool - true/false if this item is currently published
- AnyProperty dynamic, but actually bool | string | decimal | datetime | List any normal property of the content-item can be accessd directly. It's correctly .net typed (string, etc.)
Tip
In 2sxc 10.27 any property that returns a List<DynamicEntity>
now returns a containing a list.
This means that if you expect the list to just return one item, you can directly access its properties like this:
Content.Author.FirstName
.
To otherwise enumerate the items, we recommend so AsList(Content.Tags)
[!include"Razor Tutorials"]
The following properties/methods exist, but shouldn't be used. They are documented here so that you know that they are not meant for public use:
- Created - the created date
- Modified - the modified date
- Owner - the current owner of the item, usually the author
- Metadadata - currently use
AsEntity(theObject).Metadata
- Permissions - permissions of the current item (if any are defined)
- Introduced in 2sxc 01.00
- Changed to use interface IDynamicEntity in 6.x
- Draft/Published introduced in 2sxc 7.x
- Presentation introduced in 2sxc 7.x
- Modified introduced in 2sxc 8.x
- Implemented .net equality comparer in 2sxc 9.42
- Parents added in 2sxc 9.42
- Get added in 2sxc 9.42 and added to interface IDynamicEntity in 10.07
- Parents introduced in 2sxc 9.42, and added to interface IDynamicEntity in 10.07
- IsDemoItem property added in 2sxc 10.06
- Changed dynamic access to a property to return a DynamicEntity which is enumerable in 10.27